//
//  FCCenterButton.m
//  inherit-继承相关
//
//  Created by 吴剑川 on 2018/11/7.
//  Copyright © 2018 wjc. All rights reserved.
//

#import "FCCenterButton.h"
#import "UIView+viewExtension.h"
#import "WJCDefine.h"
@interface FCCenterButton()
@property (nonatomic,strong) NSMutableArray *imgArr;
@end
@implementation FCCenterButton
-(NSMutableArray *)imgArr{
    if (!_imgArr) {
        _imgArr = [[NSMutableArray alloc] init];
    }
    return _imgArr;
}
-(void)setImage:(UIImage *)image forState:(UIControlState)state{
//    [super setImage:image forState:state];
    if (self.imgArr.count<1) {
        for (int i = 0; i <UIControlStateFocused; i++) {
            [self.imgArr addObject:[UIImage new]];
        }
    }
    if (!self.centerImg) {
        self.centerImg=[[UIImageView alloc]init];
    }
    if(image){
    [self.imgArr setObject:image atIndexedSubscript:state];
    }
    self.centerImg.image=image;
    self.centerImg.contentMode=UIViewContentModeScaleAspectFit;
    self.centerImg.frame= CGRectMake(self.width*0.5-self.height*0.15, 12, self.height*0.3, self.height*0.3);
    
    [self addSubview:self.centerImg];
}
-(void)setSelected:(BOOL)selected{
    super.selected = selected;
    if (selected) {
         [self.centerImg setImage:self.imgArr[UIControlStateSelected]];
    }else{
         [self.centerImg setImage:self.imgArr[UIControlStateNormal]];
    }
       
}
-(void)setTitle:(NSString *)title forState:(UIControlState)state{
    if (!self.label) {
        self.label=[[UILabel alloc]init];
    }
    self.titleLabel.text=title;
    self.titleLabel.hidden=YES;
    self.label.frame=CGRectMake(0, self.height*0.3+20, self.width, 15);
    self.label.text=title;
    self.label.font=font13;
    self.label.textAlignment=NSTextAlignmentCenter;
     [self addSubview:self.label];
}
-(void)setTitleColor:(UIColor *)color forState:(UIControlState)state{
    if (!self.label) {
        self.label=[[UILabel alloc]init];
    }
    self.label.textColor=color;
}
-(void)setBadgeLabel:(UILabel *)badgeLabel{
    if (!_badgeLabel) {
        _badgeLabel=badgeLabel;
        _badgeLabel.frame=CGRectMake(self.centerImg.x+self.centerImg.width-8, 0, 16, 16);
        _badgeLabel.font=font12;
        _badgeLabel.backgroundColor=[UIColor redColor];
        WJCSETLRViewBorderRadius(_badgeLabel, 8);
         _badgeLabel.textColor=[UIColor whiteColor];
        badgeLabel.textAlignment=NSTextAlignmentCenter;
        [self addSubview:_badgeLabel];
    }
    if (_badgeView&&!_badgeView.hidden) {
        _badgeView.hidden=YES;
    }
}
-(void)setbadgeLabelTxt:(NSString *)txt{
    if (!self.badgeLabel) {
        self.badgeLabel=[[UILabel alloc]init];
    }
    _badgeView.hidden=YES;
    _badgeLabel.text=txt;
    [_badgeLabel sizeToFit];
    _badgeLabel.height=16;
    if(txt.length>1){
        _badgeLabel.width=(_badgeLabel.width>16?_badgeLabel.width:16)+3;
    }else{
        _badgeLabel.width=16;
    }
    if (txt.length<1) {
        _badgeLabel.hidden=YES;
    }else{
        _badgeLabel.hidden=NO;
    }
}
-(void)showBadgeView{
     if (!self.badgeView) {
         self.badgeView=[UIView new];
     }
    self.badgeView.hidden=NO;
    self.badgeLabel.hidden=YES;
}
-(void)setBadgeView:(UIView *)badgeView{
    if (!_badgeView) {
        _badgeView=badgeView;
        _badgeView.frame=CGRectMake(self.centerImg.x+self.centerImg.width-3, 5, 6, 6);
        badgeView.backgroundColor=[UIColor redColor];
        WJCSETLRViewBorderRadius(_badgeView, 3);

        [_badgeView removeFromSuperview];
        [self addSubview:_badgeView];
    }if (_badgeLabel&&!_badgeLabel.hidden) {
        _badgeLabel.hidden=YES;
    }
}
-(void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    self.centerImg.frame= CGRectMake(self.width*0.5-self.height*0.25, 8, self.height*0.5, self.height*0.5);
    self.label.frame=CGRectMake(0, self.height-15, self.width, 15);
    _badgeView.frame=CGRectMake(self.centerImg.x+self.centerImg.width-3, 5, 6, 6);
    _badgeLabel.frame=CGRectMake(self.centerImg.x+self.centerImg.width-8, 0, 16, 16);
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
